import TileMap = TiledMap.TileMap;
class Game extends BaseScene implements eui.UIComponent {



	public g_map: eui.Group;
	public i_mapbg: eui.Image;
	public player: eui.Rect;
	public Tab_UserInfo: eui.TabBar;
	public viewStack: eui.ViewStack;
	public g_controller: eui.Group;
	public b_up: eui.Button;
	public b_right: eui.Button;
	public b_down: eui.Button;
	public b_left: eui.Button;
	public bag: Bag;






	private that: any = this;
	private loading: Loading;
	private xy: eui.Label;

	private vj: VirtualJoystick = new VirtualJoystick();  //虚拟摇杆


	private speedX = 0;         //人物移动速度
	private speedY = 0;
	private speed = 10;

	private mobileMap: MobileMap;
	public constructor() {
		super();
	}
	private playerAnimaSprite;
	public menuAnimation: egret.tween.TweenGroup;

	public onCreated() {
		super.onCreated();
		this.loading = Loading.create(this);
		// this.addChild(this.vj);
		// this.vj.addEventListener("vj_start", this.onStart, this);
		// this.vj.addEventListener("vj_move", this.onChange, this);
		// this.vj.addEventListener("vj_end", this.onEnd, this);
		var playerAnima = AnimaUtil.showMoiveWithAction("Hum20", "1_0", this, function () {

		}, false, true);
		this.playerAnimaSprite = playerAnima;
		// eui.ItemTapEvent.ITEM_TAP
		this.Tab_UserInfo.addEventListener(eui.ItemTapEvent.ITEM_TAP, (e: eui.ItemTapEvent) => {
			console.log('[INFO] e.data;:' + e.item);
			switch (e.itemIndex) {
				case 0:
					playerAnima.gotoAndPlay("1_2", -1);
					break;
				case 1:
					playerAnima.gotoAndPlay("1_4", -1);
					break;
				case 2:
					playerAnima.gotoAndPlay("1_6", -1);
					break;
				case 3:
					playerAnima.gotoAndPlay("1_0", -1);
					break;
			}
		}, this.Tab_UserInfo)

		//初始化我的背包
		var bagData = [{ text: "衣服1", image: "Item_1395", anima: "Hum20" }, { text: "衣服2", image: "Item_1394", anima: "Hum0" }, { text: "衣服2", image: "Item_1394", anima: "Hum0" }];
		this.bag.setItemData(bagData, (e: eui.ItemTapEvent) => {
			Toast.show("on item click	" + e.itemIndex);

			let itemData = e.item;


			this.playerAnimaSprite && (this.removeChild(this.playerAnimaSprite));
			this.playerAnimaSprite = AnimaUtil.showMoiveWithAction(bagData[e.itemIndex].anima, "1_0", this, function () {

			}, false, true);
		});

		this.mobileMap = new MobileMap();
		this.g_map.addChild(this.mobileMap);
		this.mobileMap.createTileMap(330, 245);
	}


	public onClick(name: string, v: egret.DisplayObject) {
		if (name == "n1") {
			var xy = this.mobileMap.randomMove();

			Toast.show("随机传送到(" + xy.x + "," + xy.y + ")");
			this.xy.text = xy.x + "," + xy.y;
		} else if (name == "n2") {
			var xy = this.mobileMap.goSafeArea();
			this.xy.text = xy.x + "," + xy.y;
			Toast.show("回程(" + xy.x + "," + xy.y + ")");
		} else if (name == "player") {
			SceneManager.back();
		}


		var mapping = {
			u: Direction.Up,
			r: Direction.Right,
			d: Direction.Down,
			l: Direction.Left,
		}
		var mappingAnimation = {
			u: "0",
			r: "2",
			d: "4",
			l: "6",
		}

		if (v == this.b_up || v == this.b_down || v == this.b_right || v == this.b_left) {
			this.mobileMap.move(mapping[(<eui.Button>v).label]);
			

			this.playerAnimaSprite && (this.removeChild(this.playerAnimaSprite));
			this.playerAnimaSprite && (this.playerAnimaSprite = AnimaUtil.showMoiveWithAction(
				"Hum20",
				"2_" + mappingAnimation[(<eui.Button>v).label],
				this,
				function () {
					this.playerAnimaSprite && (this.removeChild(this.playerAnimaSprite));
					this.playerAnimaSprite && (this.playerAnimaSprite = AnimaUtil.showMoiveWithAction(
						"Hum20",
						"0_" + mappingAnimation[(<eui.Button>v).label],
						this,
						function () {

						},
						false,
						true));
				}.bind(this),
				false,
				false));
			
		}

		return true;
	}	//摇杆启动，人物开始根据摇杆移动
	private onStart() {
		this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
	}

	//触摸摇杆的角度改变，人物的移动速度方向也随之改变
	private onChange(e: egret.Event) {
		var angle = e.data;
		// this.speedX = Math.cos(angle) * this.speed;
		// this.speedY = Math.sin(angle) * this.speed;
	}

	//停止摇杆，人物停止移动
	private onEnd() {
		this.removeEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
	}

	//每帧更新，人物移动
	private onEnterFrame() {
		// this.player.x += this.speedX;
		// this.player.y += this.speedY;
	}


}